/*
 * Created on 29.08.2007
 */
package at.fhv.dcs.aw.j2me.lcdui.game;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;


public class Labyrinth extends GameCanvas implements Runnable {

    private static final int DELTA_MOVEMENT = 2;
    
    LabyrinthMIDlet m_midlet;
    Sprite m_ball;
    TiledLayer m_field;
    TiledLayer m_background;
    
    int m_viewX;
    int m_viewY;
    
    protected Labyrinth(boolean suppressKeyEvents, LabyrinthMIDlet midlet) {
        super(suppressKeyEvents);
        
        m_midlet = midlet;
        
        try {
            m_ball = new Sprite(Image.createImage(getClass().getResourceAsStream("ballframes.png")), 60, 45);
            m_ball.setPosition(30, 30);
            
            String[] layout = { 
                                "###############################",
                                "#                             #",
                                "#                             #",
                                "#                             #",                                
                                "#   ####     #       ##########",
                                "#                             #",
                                "#                             #",
                                "#                             #",
                                "#                             #",
                                "#           ######            #",
                                "#                             #",
                                "#                             #",                                
                                "#                             #",                                
                                "###############################",
                              };
            m_field = new TiledLayer(layout[0].length(), layout.length, Image.createImage(getClass().getResourceAsStream("labyrinth.png")), 25, 25);
            m_background = new TiledLayer(layout[0].length(), layout.length, Image.createImage(getClass().getResourceAsStream("labyrinth.png")), 25, 25);
            
            for (int i = 0; i < layout.length; i++) {
                for (int j = 0; j < layout[i].length(); j++) {
                    if (layout[i].charAt(j) == '#') {
                        m_field.setCell(j, i, 1);
                    } else {
                        m_background.setCell(j, i, 2);
                    }
                }
            }
            
            m_viewX = 0;
            m_viewY = 0;
            
        } catch (IOException ex) {           
        }
    }
    
    public void run() {
        Graphics g = getGraphics();
        LayerManager mgr = new LayerManager();
        mgr.append(m_ball);
        mgr.append(m_field);
        mgr.append(m_background);
        
        Thread ballRotation = new Thread() { 
            public void run() {
                while (true) {
                    m_ball.nextFrame();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        ballRotation.start();
        
        while (true) {
            
            mgr.paint(g, 0, 0);
            flushGraphics();
            
            int keys = getKeyStates();
            
            if (((keys & LEFT_PRESSED) |  (keys & RIGHT_PRESSED) | (keys & UP_PRESSED) | (keys & DOWN_PRESSED)) != 0) {
                int dx = 0;
                int dy = 0;
                
                if ((keys & LEFT_PRESSED) != 0) {
                    dx = -DELTA_MOVEMENT;
                } else if ((keys & RIGHT_PRESSED) != 0) {
                    dx = DELTA_MOVEMENT;
                } else if ((keys & UP_PRESSED) != 0) {
                    dy = -DELTA_MOVEMENT;
                } else if ((keys & DOWN_PRESSED) != 0) {
                    dy = DELTA_MOVEMENT;
                }
                
                if ((m_ball.getX() < m_viewX + m_ball.getWidth() * 2 ) ||
                    (m_ball.getX() > getWidth() + m_viewX - m_ball.getWidth() * 2) ||
                    (m_ball.getY() < m_viewY + m_ball.getHeight() * 2 ) ||
                    (m_ball.getY() > getHeight() + m_viewY - m_ball.getHeight() * 2)) {
                        m_viewX += dx;
                        m_viewY += dy;

                        mgr.setViewWindow(m_viewX, m_viewY, getWidth(), getHeight());
                        
                        g.setColor(0xffffff);
                        g.fillRect(0, 0, getWidth(), getHeight());
                } else {
                    g.setColor(0xffffff);
                    g.fillRect(m_ball.getX(), m_ball.getY(), m_ball.getWidth(), m_ball.getHeight());
                }
                
                m_ball.move(dx, dy);
            }
            
            if (m_ball.collidesWith(m_field, true)) {
                m_midlet.showYouDie();
                return;
            }
        }        
    }
}
